#include "stdafx.h"

#include "shift-2d-grid.h"

#define BOOST_TEST_MODULE shift-2d-grid-test
#include <boost/test/unit_test.hpp>
#include <vector>

std::vector<int> flatten(const std::vector<std::vector<int>> &grid) {
    ptrdiff_t rowCount = grid.size();
    if (rowCount == 0) {
        return {};
    }
    ptrdiff_t colCount = grid[0].size();
    if (colCount == 0) {
        return {};
    }

    std::vector<int> results{};

    // loop invariant:
    // [0, row) has done
    // results is the result in the done range
    for (ptrdiff_t row = 0; row < rowCount; ++row) {
        // loop invariant:
        // [0, col) has done
        for (ptrdiff_t col = 0; col < colCount; ++col) {
            int val = grid[row][col];
            results.push_back(val);
        }
        // post condition: [0, colCount) has done
    }
    // post condition:
    // [0, rowCount) has done

    return results;
}

BOOST_AUTO_TEST_CASE(sample1) {
    std::vector<std::vector<int>> inputGrid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
    int inputK = 1;

    std::vector<std::vector<int>> output = Solution{}.shiftGrid(inputGrid, inputK);
    std::vector<std::vector<int>> correctAnswer = { {9, 1, 2}, {3, 4, 5}, {6, 7, 8} };

    std::vector<int> flattenOutput = flatten(output);
    std::vector<int> flattenCorrectAnswer = flatten(correctAnswer);

    BOOST_CHECK_EQUAL_COLLECTIONS(flattenOutput.begin(), flattenOutput.end(), flattenCorrectAnswer.begin(), flattenCorrectAnswer.end());
}